import request from '@/utils/request'
import { otherFiltersItem } from '@/utils'
/**
 * 获取附件
 * @param {*} modelId
 * @returns
 */
export function getShopAttachment(modelId) {
  return request({
    url: `/api/v1/shopAttachment/query/${modelId}`,
    method: 'GET'
  })
}
/**
 * 修改附件
 * @param {*} data
 * @returns
 */
export function insertFile(data, modelId) {
  return request({
    url: `/api/v1/shopAttachment/batchUpdate?modelId=${modelId}`,
    method: 'put',
    data
  })
}

/**
 * 上传附件-批量
 * @param {*} data
 * @returns
 */
export function fileUploadBatch(data) {
  return request({
    url: '/api/v1/file/batchUploadFile',
    method: 'post',
    data
  })
}
/**
 * 获取异常配置id
 * @param {*} data
 * @returns
 */
export function getExceptionType(type, belongModel) {
  const data = new FormData()
  belongModel ? data.append('filtersRaw', JSON.stringify([otherFiltersItem('exceptionType', type, '='), otherFiltersItem('belongModel', belongModel, '=')])) : data.append('filtersRaw', JSON.stringify([otherFiltersItem('exceptionType', type, '=')]))
  return request({
    url: `/api/v1/configExceptionHandle/page`,
    method: 'post',
    data
  })
}

/**
 * 获取门店建设项目号下的详情
 * @param {*} constCode
 * @returns
 */
export function getConstData(constCode) {
  const data = new FormData()
  data.append('filtersRaw', JSON.stringify([otherFiltersItem('constCode', constCode, '=')]))
  return request({
    url: `/api/v1/shopConstruct/page`,
    method: 'post',
    data
  })
}

/**
 * 根据门店建设号获取列表页数据（无权限）
 * @param {*} constCode
 * @returns
 */
export function searchDesignTime(constCode) {
  const data = new FormData()
  data.append('filtersRaw', JSON.stringify([otherFiltersItem('constCode', constCode, '=')]))
  return request({
    url: '/api/v1/shopDesign/getShopDesign/noPermission/page',
    method: 'POST',
    data
  })
}
// 验证当前选择门店是否存在测绘任务
export function taskValide(data) {
  return request({
    url: `/api/v1/shopSurvey/checkShopSurvey`,
    method: 'post',
    data
  })
}
/**
 * 获取附件配置
 * @param {*} data
 * @returns
 */
export function postConfigAttachmentTemplate(data) {
  return request({
    url: '/api/v1/configAttachmentTemplate/page',
    method: 'POST',
    data
  }).then(res => {
    if (res.data.records.length === 0) {
      return Promise.reject(res)
    }
    return res
  })
}

/**
 * 获取门店附件
 * @param {*} modelId
 * @param {*} configAttachmentId
 * @returns
 */
export function postShopAttahment(modelId, configAttachmentId) {
  const data = new FormData()
  data.append('pageFlag', false)
  const filtersRaw = []
  if (configAttachmentId) {
    filtersRaw.push(otherFiltersItem('configAttachmentId', configAttachmentId, '='))
  }
  filtersRaw.push(otherFiltersItem('modelId', modelId, '='))
  data.append('filtersRaw', JSON.stringify(filtersRaw))
  return request({
    url: '/api/v1/shopAttachment/page',
    method: 'POST',
    data
  }).then(res => {
    return res.data.records
  })
}
/**
 * 删除附件
 * @param {*} id
 * @returns
 */
export function deleteShopAttachment(id) {
  return request({
    url: `/api/v1/shopAttachment/${id}`,
    method: 'delete'
  })
}
/**
 * 批量删除门店附件
 * @param {*} ids
 * @returns
 */
export function deleteBatch(ids) {
  return request({
    url: `/api/v1/shopAttachment/deleteBatch`,
    method: 'delete',
    data: ids
  })
}
/**
 * 编辑附件
 * @param {*} data
 * @returns
 */
export function putShopAttachment(data) {
  return request({
    url: '/api/v1/shopAttachment',
    method: 'PUT',
    data
  })
}

/**
 * 根据门店下的客户code查询 对应的经销商信息
 * @returns
 */
export function searchCustomer(code, permissionCode) {
  const data = new FormData()
  data.append('filtersRaw', JSON.stringify([otherFiltersItem('code', code, '=')]))
  return request({
    url: '/select-service/api/v1/customer/page',
    headers: {
      'permission-code': permissionCode,
      'Permission-App': 'slm'
    },
    method: 'POST',
    data
  })
}

/**
 * 根据门店下的客户code查询 对应的业务账号信息
 * @returns
 */
export function searchBizAccount(code, permissionCode) {
  const data = new FormData()
  data.append('filtersRaw', JSON.stringify([otherFiltersItem('customerCode', code, '=')]))
  return request({
    url: '/select-service/api/v1/account/page',
    headers: {
      'permission-code': permissionCode,
      'Permission-App': 'slm'
    },
    method: 'POST',
    data
  })
}
/**
 * 根据门店下的客户code查询 对应的业务账号信息
 * @returns
 */
export function getBusinessAccount(code, permissionCode) {
  const data = new FormData()
  data.append('filtersRaw', JSON.stringify([otherFiltersItem('customerCode', code, '=')]))
  return request({
    url: '/select-service/api/v1/account/page',
    headers: {
      'permission-code': permissionCode,
      'Permission-App': 'slm'
    },
    method: 'POST',
    data
  })
}

/**
 * 根据业务账号查询客户经理
 * @returns
 */
export function searchManagerAccount(code) {
  const data = new FormData()
  data.append('filtersRaw', JSON.stringify([otherFiltersItem('accounts', code, '=')]))
  return request({
    url: '/api/v1/user/listCustomerManagerByAccounts',
    method: 'POST',
    data
  })
}

/**
 * 获取所有人员信息
 * @returns
 */
export function getSurveyorData(data) {
  return request({
    url: '/api/v1/user/domainUser/page',
    method: 'POST',
    data
  })
}
// 申请延期  异常
export function saveException(data) {
  return request({
    url: `/api/v1/shopException`,
    method: 'post',
    data
  })
}
/**
 * 查询日志
 * @param {*} sourceId
 * @returns
 */
export function postLogPage(sourceId) {
  const data = new FormData()
  data.append('pageFlag', false)
  data.append('filtersRaw', JSON.stringify([otherFiltersItem('sourceId', sourceId, '=')]))
  return request({
    url: '/api/v1/log/page',
    method: 'POST',
    data
  })
}
/**
 * 下载文件
 * @param {*} fileName
 * @returns
 */
export function downloadFile(fileName) {
  return request({
    url: `/api/v1/file/downloadFile?fileName=${fileName}`,
    method: 'POST',
    responseType: 'blob'
  })
}
/**
 * 批量查询附件
 * @param {*} ids
 * @returns
 */
export function getBatchFile(ids) {
  const data = new FormData()
  data.append('pageFlag', false)
  const filtersRaw = []
  filtersRaw.push(otherFiltersItem('modelId', `[${ids.join(',')}]`, 'in'))
  data.append('filtersRaw', JSON.stringify(filtersRaw))
  return request({
    url: '/api/v1/shopAttachment/page',
    method: 'POST',
    data
  })
}

/**
 * 区域配置列表
 * @param {*}
 * @returns
 */
export function getAreaList() {
  const data = new FormData()
  data.append('pageFlag', false)
  const filtersRaw = []
  filtersRaw.push(otherFiltersItem('enable', '1', '='), otherFiltersItem('dr', '0', '='))
  data.append('filtersRaw', JSON.stringify(filtersRaw))
  return request({
    url: `/api/v1/configArea/page`,
    method: 'POST',
    data
  })
}

/**
 *
 * @param {*} account
 * @returns
 */
export function getOrgByAccount(account) {
  return request({
    url: `/api/v1/user/userOrgByAccount/?account=${account}`,
    method: 'get'
  })
}

/**
 * 删除附件
 * @param {String} id
 * @returns
 */
export function deletFileData(id) {
  return request({
    url: `/api/v1/shopAttachment/${id}`,
    method: 'delete'
  })
}

/**
 * 获取验收任务的详情
 * @param {*} id
 * @returns
 */
export function getAcceptTaskDetail(id) {
  return request({
    url: `/api/v1/acceptTask/${id}`,
    method: 'get'
  })
}

/**
 * 获取验收任务环节
 * @param {*} id 验收任务id
 * @returns
 */
export function getAcceptTaskStep(id) {
  const data = new FormData()
  data.append('pageFlag', false)
  data.append('order', 'asc')
  data.append('sort', 'receiveTime')
  data.append('filtersRaw', JSON.stringify([otherFiltersItem('acceptId', id, '=')]))
  return request({
    url: `/api/v1/acceptTaskDetail/page`,
    method: 'post',
    data
  })
}

/**
 *
 * @param {*}
 * @returns
 */
export function getAcceptRectifyTask(acceptTaskCode) {
  // const data = new FormData()
  // data.append('pageFlag', false)
  // data.append('filtersRaw', JSON.stringify([otherFiltersItem('acceptTaskCode', acceptTaskCode, '=')]))
  return request({
    url: `/api/v1/shopAccept/problemDetails/?id=${acceptTaskCode}`,
    method: 'get'
  })
}

/**
 * input-with-dialog字段数据
 * @param {*} method 请求方式
 * @param {*} url 请求地址
 * @param {*} params 参数
 * @param {*} header 请求头
 * @returns
 */
export function getFiledData(method, url, params, header) {
  return request({
    url,
    method,
    data: params
  })
}

/**
 * 根据图片导出PPT
 * @param {FormData} data
 * @returns
 */
export function exportPPT(data) {
  return request({
    url: '/api/v1/acceptProblem/pptExport',
    method: 'post',
    headers: {
      // 'Permission-Percode': 'SURVEY_DESIGN',
      // 'Permission-App': 'slm'
    },
    data,
    responseType: 'blob'
  })
}
